package com.situ.mall.user.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.situ.mall.common.util.Result;
import com.situ.mall.pojo.entity.user.User;
import com.situ.mall.user.pojo.query.UserQuery;
import com.situ.mall.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CKX
 * @since 2024-10-14
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    public Result<User> login(@RequestBody User user) {
        //Result<User> result = userService.login(user);
        //return result;
        User loginUser = userService.login(user);
        if (loginUser.getStatus() == 0) {
            return Result.error("用户已被禁用");
        } else {
            return Result.ok("登录成功", loginUser);
        }
    }

    @GetMapping("/list")
    public Result list(UserQuery userQuery) {
        Map<String, Object> map = userService.selectByPage(userQuery);
        return Result.ok(map);
    }

    @PutMapping("update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.ok("更新成功");
    }

    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        userService.updateStatus(id, status);
        return Result.ok("更新成功");
    }

    @GetMapping("/selectById/{id}")
    public Result<User> selectById(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.ok(user);
    }

    @GetMapping("/selectAll")
    public Result<List<User>> selectAll() {
        List<User> list = userService.list();
        return Result.ok(list);
    }

    @PostMapping("/findUserName")
    public Result findUserName(@RequestBody User user) {
        Boolean isHadUser = userService.findUserName(user);
        if (isHadUser) {
            return Result.error("该用户已被注册");
        } else
            return Result.ok("该用户可以注册");
    }

    @PostMapping("/findUserPassword")
    public Result findUserPassword(@RequestBody User user) {
        Boolean isPasswordTrue = userService.findUserPassword(user);
        if (isPasswordTrue) {
            return Result.ok("原密码正确");
        } else {
            return Result.error("原密码错误");
        }
    }

    @PutMapping("/register")
    public Result register(@RequestBody User user) {
        userService.save(user);
        return Result.ok("注册成功");
    }

    @PutMapping("/reCharge")
    public Result reCharge(@RequestBody User user) {
        userService.reCharge(user);
        return Result.ok("充值成功");
    }

    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody User user) {
        userService.updatePassword(user);
        return Result.ok("修改成功");
    }
}

